//
//  ShardCell.m
//  Pianke
//
//  Created by BetaTheta on 16/1/18.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
#import "ShardCell.h"
@interface ShardCell ()
@property (nonatomic, retain)UIButton *commentBtn;
@property (nonatomic, retain)UIButton *likeBtn;
@property (nonatomic, retain)UIImageView *tagImg;
@end
@implementation ShardCell
- (void)dealloc
{
    [_iconImg release];
    [_nameLabel release];
    [_timeLabel release];
    [_coverImg release];
    [_coverImg_wh release];
    [_contentLabel release];
    [_tagImg release];
    [_tagLabel release];
    [_tagCount release];
    [_commentBtn release];
    [_commentCount release];
    [_likeBtn release];
    [_likeCount release];
    [super dealloc];
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //
        self.iconImg = [[UIImageView alloc] init];
//        _iconImg.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_iconImg];
        
        self.nameLabel = [[UILabel alloc] init];
//        _nameLabel.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_nameLabel];
        
        self.coverImg = [[UIImageView alloc]init];
//        _coverImg.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_coverImg];
        
        
        self.timeLabel = [[UILabel alloc] init];
//        _timeLabel.backgroundColor = [UIColor orangeColor];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_timeLabel];
        
        self.contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:15];
//        _contentLabel.backgroundColor = [UIColor purpleColor];
        [self.contentView addSubview:_contentLabel];
        
        // 标签
        self.tagImg = [[UIImageView alloc] init];
        _tagImg.image = [UIImage imageNamed:@"icon_biaoqian.png"];
        [self.contentView addSubview:_tagImg];
        
        self.tagLabel = [[UILabel alloc] init];
//        _tagLabel.backgroundColor = [UIColor darkGrayColor];
        _tagLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_tagLabel];
        
        self.tagCount = [[UILabel alloc] init];
//        _tagCount.backgroundColor= [UIColor greenColor];
        _tagCount.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_tagCount];
        
        // 评论
        self.commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _commentBtn.backgroundColor = [UIColor cyanColor];
        [_commentBtn setImage:[UIImage imageNamed:@"icon_pinglun.png"] forState:UIControlStateNormal];
        [self.contentView addSubview:_commentBtn];
        
        self.commentCount = [[UILabel alloc] init];
//        _commentCount.backgroundColor = [UIColor blueColor];
        _commentCount.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_commentCount];
        
        // 喜欢
        self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _likeBtn.backgroundColor = [UIColor brownColor];
        [_likeBtn setImage:[UIImage imageNamed:@"icon_xihuan"] forState:(UIControlStateNormal)];
        [self.contentView addSubview:_likeBtn];
        
        self.likeCount = [[UILabel alloc] init];
//        _likeCount.backgroundColor = [UIColor magentaColor];
        _likeCount.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_likeCount];
        
    }
    return self;
}
// 计算文本高度方法
+ (CGFloat)heightWithText:(NSString *)text
{
    // 保存文本属性(大小)
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    // 预设尺寸 文本最大不超过这个尺寸
    // 根据设置的宽度计算高度(宽度和label保持一致)
    CGSize size = CGSizeMake(414 - 20, 1000);//?? 414
    // 根据文本 计算出一个CGRect
    // 参数1: 尺寸范围
    // 参数2: 文本属性
    // 参数3: 按照什么方式获取rect
    // 参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    // 返回得到的高度
    return rect.size.height;
}
// 计算文本宽度类方法
+ (CGFloat)widthWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:13]};
    CGSize size = CGSizeMake(300, 10);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.width;
}
// 计算图片高度方法
+ (CGFloat)heightWithImage:(NSString *)str
{
    if (str.length == 0) {
        return 0;
    } else {
        // 拆分
        NSArray *rectArr = [str componentsSeparatedByString:@"*"];
        // 计算高度
        CGFloat h = [rectArr.lastObject floatValue] * 375 / [rectArr.firstObject floatValue];
        return h;
    }
}
// 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    _iconImg.frame = CGRectMake(20, 20, 40, 40);
    _iconImg.layer.cornerRadius = _iconImg.frame.size.width / 2;
    _iconImg.clipsToBounds = YES;
    
    _nameLabel.frame = CGRectMake(65, 30, 200, 30);
    
    _timeLabel.frame = CGRectMake(WIDTH - 100, 30, 80, 30);
    
    
    // imgH->图片高度 textH->文本高度 tagTextW->tag内容宽度
    CGFloat imgH = [[self class] heightWithImage:_coverImg_wh];
    _coverImg.frame = CGRectMake(20, 70, WIDTH - 40, imgH);
    
    CGFloat textH = [[self class] heightWithText:_contentLabel.text];
    _contentLabel.frame = CGRectMake(20, 80 + imgH, WIDTH - 40, textH);
    
    
    CGFloat tagTextW = [[self class] widthWithText:_tagLabel.text];
    _tagLabel.frame = CGRectMake(45, 85 + imgH + textH + 20, tagTextW , 10);
    if (_tagLabel.text.length ) {
        _tagImg.frame = CGRectMake(20, 80 + imgH + textH + 20, 20, 20);
        _tagCount.frame = CGRectMake(40 + tagTextW  + 10, 80 + imgH + textH + 20, 50, 20);
    } else{
        _tagCount.frame = CGRectMake(0, 0, 0, 0);
        _tagImg.frame = CGRectMake(0, 0, 0, 0);
    }
    _commentBtn.frame = CGRectMake(WIDTH - 150, 80 + imgH + textH + 20, 20, 20);
    CGRect f = _commentBtn.frame;
    f.origin.x += 30;
    _commentCount.frame = f;
    f.origin.x += 50;
    _likeBtn.frame = f;
    f.origin.x += 30;
    _likeCount.frame = f;
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
